package com.vae.auth.controller;

import com.vae.auth.domain.XcUser;
import com.vae.auth.service.WxAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WxLoginController {

    @Autowired
    WxAuthService wxAuthService;


    /**
     * 微信扫码登录
     * @param code
     * @param state
     * @return
     */
    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state){
        System.out.println("微信扫码回调的授权码code: 【"+code+"】; 状态state: 【"+state+"】");
        //请求微信申请令牌，拿到令牌查询用户信息，将用户信息写到数据库
        XcUser xcUser = wxAuthService.wxAuth(code);
        if(xcUser==null){
            return "redirect:http://localhost/error.html";
        }
        String username = xcUser.getUsername();
        //用户登录成功之后跳到登录页面
        return "redirect:http://localhost/sign.html?username="+username+"&authType=wx";
    }
}
